<template>
    <view>
        <!-- <home :cur="PageCur" v-if="PageCur=='home'" ref="refhome" @baike='baike' :key="commponent1Key"></home> -->
        <!-- 用户端 -->
        <home :cur="PageCur" v-if="PageCur=='home'" :key="commponent1Key"></home>
        <questions :cur="PageCur" :indecs='indecs' v-if="PageCur=='questions'" :key="commponent1Key"></questions>
        <publics :cur="PageCur" v-if="PageCur=='publics'"  ref="refhome" :key="commponent1Key"></publics>
        <test :cur="PageCur" v-if="PageCur=='test'" :key="commponent1Key"></test>
        <user1 :cur="PageCur" v-if="PageCur=='user1'"  @ordersa='ordersa' @resurl='aaa' :key="commponent1Key"></user1>

        <!-- 咨询师端 -->
        <workbench :cur="PageCur" v-if="PageCur=='workbench'"  @resurlxx='getxxlb' :key="commponent1Key"></workbench>
        <orderForm :cur="PageCur" v-if="PageCur=='orderForm'"   :key="commponent1Key"></orderForm>
        <user :cur="PageCur" v-if="PageCur=='user'"  @resurl2='aaa2' @orders='orders'  :key="commponent1Key"></user>
        <!-- 商城 -->
        <stores :cur="PageCur" v-if="PageCur=='stores'" @returns='returns' @getjifen='getjifen' @refresh="refresh" :key="commponent1Key"></stores>

        <classify :cur="PageCur" v-if="PageCur=='classify'" @returns='returns'   :key="commponent1Key"></classify>
        <pointsMall :cur="PageCur" v-if="PageCur=='pointsMall'" @returns='returns'   :key="commponent1Key"></pointsMall>
        <shoppingmall :cur="PageCur" v-if="PageCur=='shoppingmall'" @returns='returns'  @refresh="refresh"  :key="commponent1Key"></shoppingmall>

        <shoppuser :cur="PageCur" v-if="PageCur=='shoppuser'" @returns='returns'   :key="commponent1Key"></shoppuser>
        <view style="height:180rpx;width: 100vw;">

        </view>
        <foot :kessy="PageCur" @childFn="parentFn" :indecs='indecs' ref="foot"></foot>


       <view class="login_box" v-if="show">
            <view class="logins">
                <text>请再授权手机号</text>
                <button class="but" open-type="getPhoneNumber" @getphonenumber="getphonenumber">点击授权</button>
            </view>


        </view>
        <view class="login_box" v-if="show1">
             <view class="logins">
                <text>登录账号后，查看完整功能。</text>
                <button class="but" @tap="islogin11">点击微信授权</button>
            </view>
            <checkbox-group @change='change' style="display: flex;align-items: center;">
                <label>
                    <checkbox value="cb" color="#6d79fe" style="transform:scale(0.5)"/>
                </label>
                <view class="label">
                    <view class="">
                        我已同意
                    </view>
                    <view class="" style="display: flex;align-items: center;">
                        <view class="text_41" @click.stop="agreement">
                            《用户协议》
                        </view>
                        <view class="text_41" @click.stop="privacy">
                            《隐私协议》
                        </view>
                        <view class="text_41" @click.stop="liability">
                            《免责声明》
                        </view>
                    </view>
                </view>

            </checkbox-group>

        </view>
    </view>
</template>

<script>
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
    export default {
        data() {
            return {
                popup:true,
                show1: true,
                show: false,
                PageCur: 'home',
                commponent1Key: 0,
                commponent2Key: 0,
                shopList: {},
                indecs:0,
                ifexperts:'',
                spreadSpid:'',
                checked:'',

              type: 0,
            };
        },
        onLoad: function(option) {
           // console.log("dsadsadsadsad",option);
           // this.PageCur = JSON.parse(option.query).id
           // //this.$set(this.PageCur,JSON.parse(option.query).id)
           // console.log(this.PageCur)
		   if (this.$store.state.app.token != "token") {
			   this.show1 = false
		   }
           if(typeof(option.pid) != "undefined"){
               console.log(option.pid)
               uni.showToast({
                   title: option.pid
               })
                this.spreadSpid=option.pid

           }else{
               // uni.showToast({
               //     title: "测试"
               // })
           }
        },

        created(){
           //this.$store.commit('http/SAVE_USER_WX_INFO', 'https://ceshi1.cnshangji.cn/');
          //this.$store.commit('http/SAVE_USER_WX_INFO', 'http://185.227.152.2:9095/');
            this.$store.commit('http/SAVE_USER_WX_INFO', HTTP_REQUEST_URL);
            //zhjt.cnshangji.cn
             // this.vips()
             if(this.$store.state.userInfo.wxInfo!=null){
                 this.show1=false
             }
        },
		onReachBottom() {
			console.log('触发了底部');
			// this.isOver = true;
			// this.pageNum++; //页数加一
			// this.BoutiqueList(); //回调接口
		},
        methods:{

            liability(){
                uni.navigateTo({
                    url:'/pages/agreement/liability'
                })
            },
            agreement(){
                uni.navigateTo({
                    url:'/pages/agreement/agreement'
                })
            },
            privacy(){
              uni.navigateTo({
                  url:'/pages/agreement/privacy'
              })
            },
            change(e){
                console.log(e);
                this.checked=e.detail.value

                console.log(this.checked);
            },
            onPageScroll(e) {
                if(this.PageCur=='home'){
                    // this.$refs.refhome.PageScroll(e)
                }
                if(this.PageCur=='publics'){
                    this.$refs.refhome.PageScroll(e)
                }
            },
            async onReachBottom() {
                        if(this.PageCur=='home'){
                             // uni.$emit('Bottom')
                        }
                       if(this.PageCur=='publics'){
                            // uni.$emit('Bottom')
                       }
                       if(this.PageCur=='workbench'){
                         // uni.$emit('Bottom')
                       }
                       if(this.PageCur=='orderForm'){
                         // uni.$emit('Bottom')
                       }
              if(this.PageCur=='xxlb'){
                uni.$emit('Bottom')
              }
            		},
            islogin11(){
                if(this.checked.length!=0){
                    this.islogin()
                }
                else{
                    uni.showToast({
                        title: "请先同意用户协议、隐私协议、免责声明",
                        icon:"none"
                    })
                }
            },
            islogin() {
                const that = this
                //获取用户信息
                uni.getUserProfile({
                    lang: 'zh_CN',
                    desc: '获取您的昵称与头像',
                    success(res) {
                        uni.login({
                            provider: 'weixin',
                            success: function(loginRes) {
                                // code = loginRes.code
                                let form = {
                                    avatar: res.userInfo.avatarUrl,
                                    city: res.userInfo.city,
                                    code: loginRes.code,
                                    country: "",
                                    encryptedData: res.encryptedData,
                                    errMsg: res.errMsg,
                                    iv: res.iv,
                                    nickName: res.userInfo.nickName,
                                    province: '',
                                    rawData: res.rawData,
                                    sex: 0,
                                    signature: res.signature,
                                    type: "routine",
                                    userInfo: res.userInfo,
                                    spread_spid:that.spreadSpid
                                }
                                //和code组成一个对象
                                let datafrom = {
                                    form: form,
                                    code: loginRes.code
                                }
                                //调取接口
                                uni.request({
                                    url: that.$store.state.http.http +
                                        'authorize/program/login?code=' +
                                        datafrom.code, //仅为示例，并非真实接口地址。
                                    data: datafrom.form,
                                    method: 'POST',
                                    header: {
                                        'Content-Type': 'application/json' //自定义请求头信息
                                    },
                                    success: (response) => {
                                        console.log('response', response)
                                        if (response.data.data.type === "register") {
                                            uni.showToast({
                                                title: '请再授权手机号登录'
                                            })
                                            that.show1 = false
                                            that.show = true
                                            that.authKey = response.data.data.key
                                        } else if (response.data.data.type === "login") {
                                            that.show1 = false
                                            that.show = false;
                                            that.token = response.data.data.token
                                            that.$store.commit('app/SAVE_USER_TOKEN',
                                                "Bearer " + response.data.data.token);
                                            that.$store.commit(
                                                'userInfo/SAVE_USER_WX_INFO',
                                                response.data.data);
                                            console.log(that.token)
                                            that.PageCur=''
                                            setTimeout(() => {
                                            that.PageCur='home'
                                            },100);
                                             console.log(that.PageCur);
                                              that.ifexperts=response.data.data.ifexperts
                                              console.log("Ifexperts",that.ifexperts);
                                              uni.setStorage({
                                              	key: 'Ifexperts',
                                              	data: response.data.data.Ifexperts,
                                              	success: function () {
                                              		console.log('Ifexperts success');
                                              	}
                                              });
                                              uni.setStorage({
                                              	key: 'vip',
                                              	data: response.data.data.vip,
                                              	success: function () {
                                              		console.log('vip success');
                                              	}
                                              });
                                        }

                                    }
                                });
                            }
                        });
                        // 通常进入小程序就要获取用户信息
                        //定义参数

                    },
                    fail(err) {
                        console.log('err', err)
                    }
                })
            },
            getUserPhoneNumber(encryptedData, iv, code) {
                console.log(this)
                let data = {
                    encryptedData: encryptedData, //小程序获取手机号加密数据
                    iv: iv, //加密算法的初始向量
                    code: code, //验证码
                    key: this.authKey, //登录返回的key值
                    type: 'routine' //写死
                }
                uni.request({
                    url: this.$store.state.http.http + 'register/binding/phone', //仅为示例，并非真实接口地址。
                    data: data,
                    method: 'POST',
                    header: {
                        'Content-Type': 'application/json' //自定义请求头信息
                    },
                    success: (res) => {
                        uni.hideLoading();
                        this.show1 = false
                        this.show = false;
                        this.token = res.data.data.token
                        this.$store.commit('app/SAVE_USER_TOKEN',"Bearer " + res.data.data.token);
                        this.$store.commit('userInfo/SAVE_USER_WX_INFO', res.data.data);
                        console.log("获取手机号", res.data.data.token)
                        this.PageCur=''
                        setTimeout(() => {
                        this.PageCur='home'
                        },100);
                         console.log(this.PageCur);
                          this.ifexperts=res.data.data.ifexperts
                          console.log("Ifexperts",this.ifexperts);
                          uni.setStorage({
                          	key: 'Ifexperts',
                          	data: res.data.data.Ifexperts,
                          	success: function () {
                          		console.log('Ifexperts success');
                          	}
                          });
                          uni.setStorage({
                          	key: 'vip',
                          	data: res.data.data.vip,
                          	success: function () {
                          		console.log('vip success');
                          	}
                          });
                    },
                    fail: (res) => {
                        uni.hideLoading();
                        this.$util.Tips({
                            title: res
                        });
                    }
                });

                // _API_getUserPhone({
                // 		encryptedData: encryptedData, //小程序获取手机号加密数据
                // 		iv: iv,		//加密算法的初始向量
                // 		code: code,	//验证码
                // 		key: this.authKey,	//登录返回的key值
                // 		type: 'routine'	//写死
                // 	})
                // 	.then(res => {
                // 		//注册成功后返回token 储存token 进行登录
                // 		console.log("获取手机号",res)
                // 	})
                // 	.catch(res => {
                // 		uni.hideLoading();
                // 		this.$util.Tips({
                // 			title: res
                // 		});
                // 	});
            },
            // getphonenumbers(){
            //   if(this.checked.length!=0){
            //       this.getphonenumber()
            //   }else{
            //       uni.showToast({
            //           title: "请先同意用户协议、隐私协议、免责声明",
            //           icon:"none"
            //       })
            //   }
            // },
            getphonenumber(e) {
                let that = this //函数指向
                uni.showLoading({
                    title: '加载中'
                });
                var code = ''
                //获取用户code
                uni.login({
                    provider: 'weixin',
                    success: function(loginRes) {
                        that.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, loginRes
                            .code); //传参数给绑定方法
                    }
                });
            },
            aaa(data){
              console.log("sadsa",data);
              this.indecs=data;
              this.type = data
              this.PageCur='user'
            },
            aaa2(data){
              this.indecs=data;
              this.type = data
              this.PageCur='user1'
            },
            orders(){
                this.PageCur='orderForm'
            },
			ordersa(){
			    this.PageCur='user1'
			},
            getxxlb(data){
                console.log("getxxlb",data);

                this.PageCur=data
            },
            baike(data){
                  console.log("baike",data);
                   this.PageCur=data
            },
            parentFn(value) {
            	console.log(value)
            	this.PageCur = value
                if(value=='subscribe'){
                    this.indecs=2;
                    this.PageCur = 'stores'
                }
            },
            returns(){
                 this.indecs = this.type;
                 this.PageCur='home'
            }
            ,getjifen(){
                this.PageCur='pointsMall'
            },

          // 刷新购物车数量
          refresh() {
              console.log("子组件调用")
              this.$refs.foot.queryShoppingNum()
          }
        }
    }
</script>

<style lang="scss">

 .login_box {
        background: rgba(0, 0, 0, 0.7);
        padding: 20rpx 20rpx 50rpx;
        width: 100%;
        box-sizing: border-box;
        color: #fff;
        font-size: 24rpx;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 6666;
    }
    .logins{
        padding: 20rpx 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
.but {
        margin: 0;
        height: 60rpx;
        line-height: 60rpx;
        font-size: 22rpx;
        background: #FD1A16;
        color: #fff !important;
    }
    .label{
        font-size: 0.69rem;
        display: flex;
        align-items: center;
    }
    .text_41 {
    	color: #6d79fe;
    	font-family: SourceHanSansCN;
    }
</style>
